﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WTwitter.Model.Twitter.Json;
using WTwitter.Model.Twitter;
using MbUnit.Framework;
using WTwitter.Model.Framework;
using WTwitter.Model.Framework.TextSplitter;

namespace WTwitter.Tests.Model.Twitter {
	class TwitterItemTest {
		private static class TestDataFactory  {
			public static  Status CreateStatus() {
				return new Status() {
					Id = 100,
					Favorited = false,
					CreatedAtString = "Mon Jan 11 15:26:14 +0000 2010",
					Text = "おはようございます",
					InReplyToScreenName = "yuki",
					InReplyToStatusId = null,
					InReplyToUserId = null,
					Truncated = false,
					User = new User() {
						Id = 33,
						Following = false,
						Name = "yuki_",
						ScreenName = "yuki",
						Url = "http://wtwitter.codeplex.com/",
						ProfileImageUrl = "http://someimage",
						Description = "description for user",
					}
				};
			}
		}

		[Test]
		public void TestConstructor() {
			var status = TestDataFactory.CreateStatus();
			var target = new TwitterItem(status);
			Assert.AreEqual(100,target.Id);
			Assert.IsFalse(target.Favorited);
			var date = new DateTime(2010, 1, 11, 15, 26, 14, DateTimeKind.Utc);
			Assert.AreEqual(date.ToLocalTime(), target.CreatedAt, "CreatedAtはLocalTimeで持つ");
			Assert.AreEqual(ItemType.TwitterStatus, target.Type);
			Assert.AreEqual("おはようございます", target.Text);
			Assert.AreEqual("yuki", target.User.ScreenName);
			Assert.AreEqual("yuki_", target.User.Name );
			Assert.AreEqual("description for user", target.User.Description );
			
		}

		[Test]
		public void TestEquality() {
			//EqualsはIdで判断する

			var status1 = TestDataFactory.CreateStatus();
			var status2 = TestDataFactory.CreateStatus();
			var status3 = TestDataFactory.CreateStatus();
			status1.Id = 10;
			status2.Id = 10;
			status3.Id = 20;
			var item1 = new TwitterItem(status1);
			var item2 = new TwitterItem(status2);
			var item3 = new TwitterItem(status3);

			Assert.IsTrue(item1.Equals(item2));
			Assert.IsFalse(item1.Equals(item3));
		}

		[Test]
		public void TestTextComponent() {
			//細かい動作はSplitterクラスのテストで確認する

			var status1 = TestDataFactory.CreateStatus();
			var item1 = new TwitterItem(status1);
			Assert.AreEqual(1, item1.TextComponents.Count);

			var status2 = TestDataFactory.CreateStatus();
			status2.Text = "@yuki1090 てすと http://www.google.com てすと";
			var item2 = new TwitterItem(status2);
			
			//※"@" "yuki1090" " てすと " "http://www.google.com" " てすと"に分かれる
			//IDに@は含まれないので注意
			Assert.AreEqual(5, item2.TextComponents.Count);
			Assert.AreEqual(TextComponentType.Plain, item2.TextComponents[0].Type);
			Assert.AreEqual(TextComponentType.UserName, item2.TextComponents[1].Type);
			Assert.AreEqual(TextComponentType.Plain, item2.TextComponents[2].Type);
			Assert.AreEqual(TextComponentType.Url, item2.TextComponents[3].Type);
			Assert.AreEqual(TextComponentType.Plain, item2.TextComponents[4].Type);
			Assert.AreEqual("@", item2.TextComponents[0].Text);
			Assert.AreEqual("yuki1090", item2.TextComponents[1].Text);
			Assert.AreEqual(" てすと ", item2.TextComponents[2].Text);
			Assert.AreEqual("http://www.google.com", item2.TextComponents[3].Text);
			Assert.AreEqual(" てすと", item2.TextComponents[4].Text);
		}
	}
}
